package com.zhaowenhao.hello.spring.framework.webmvc.servlet;

import java.io.File;
import java.util.Locale;

/**
 * @Description: 专门解析 html
 * @Author: zwh
 * @Date: 2020-08-04
 * @Time: 16:18
 */
public class WHViewResolver {
    private final String DEFAULT_TEMPLATE_SUFFIX = ".html";

    private File templateRootDir = null;

    public WHViewResolver(String templateRoot) {
        String templateDir = this.getClass().getClassLoader().getResource(templateRoot).getFile();
        templateRootDir = new File(templateDir);
    }

    public WHView resolveViewName(String viewName, Locale locale) {
        if ("".equals(viewName) || viewName == null) {
            return null;
        }
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFIX) ? viewName : viewName + DEFAULT_TEMPLATE_SUFFIX;
        File viewFile = new File((templateRootDir.getPath() + "/" + viewName).replaceAll("/+", "/"));
        return new WHView(viewFile);
    }


}
